<template>

  <a-modal
    :title="'配置'+addTitle"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div class="salesCard">
      <a-form :form="form">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '0px', paddingLeft: '16px'}">
          <a-tab-pane loading="true" tab="基本配置" key="1" style="margin-top: 35px">
            <a-form-item
              label="采集数据模式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micDeviceType"
            >
              <a-radio-group v-decorator="['micDeviceType']">
                <a-radio value="tcp">tcp</a-radio>
                <a-radio value="ftp">ftp</a-radio>
                <a-radio value="usb">usb</a-radio>
                <a-radio value="usb-8">usb-8</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="USB模式声卡名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micDeviceName"
              has-feedback
            >
              <a-input
                placeholder="请输入USB模式声卡名称"
                v-decorator="['micDeviceName']"/>
            </a-form-item>

            <a-form-item
              label="是否有1：N过程"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micUsbMode"
            >
              <a-radio-group
                v-decorator="['micUsbMode',{ initialValue: '2' }]">
                <a-radio value="1">有1：N过程</a-radio>
                <a-radio value="2">无1：N过程</a-radio>
              </a-radio-group>
            </a-form-item>


            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="每小时上传文件个数"
              v-show="!!setData.micUploadFileQuantity"
            >
              <a-input-number
                placeholder="0"
                style="width: 100%"
                v-decorator="['micUploadFileQuantity', { initialValue: '0' }]"
                :min="0"
                :max="100"
                :step="1"/>
            </a-form-item>

            <a-form-item
              label="文件大小"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audioWavSize"
              has-feedback
            >
              <a-radio-group v-decorator="['audioWavSize', { initialValue: '224046' }]">
                <a-radio value="224046">7s</a-radio>
                <a-radio value="320046">10s</a-radio>
                <a-radio value="480046">15s</a-radio>
                <a-radio value="960046">30s</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="临时文件目录"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
              v-show="!!setData.audioTmpSavePath"
            >
              <a-input
                placeholder="请输入临时文件目录"
                v-decorator="['audioTmpSavePath']"/>
            </a-form-item>

            <a-form-item
              label="备份文件目录"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audioBackupPath"
              has-feedback
            >
              <a-input
                placeholder="请输入备份文件目录"
                v-decorator="['audioBackupPath']"/>
            </a-form-item>

            <a-form-item
              label="发送网卡IP"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.appNetworkInterface"
              has-feedback
            >
              <a-input
                placeholder="请输入发送网卡IP"
                v-decorator="['appNetworkInterface']"/>
            </a-form-item>

            <a-form-item
              label="发送设备CPU温度"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.appReportStartupStatus"
            >
              <a-radio-group
                v-decorator="['appReportStartupStatus']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>

            </a-form-item>

            <a-form-item
              label="上传正常数据"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.uploadNormalStatus"
            >
              <a-radio-group
                v-decorator="['uploadNormalStatus']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>

            </a-form-item>

            <a-form-item
              label="发送启动日志信息"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.appUploadTemperatureOnOff"
            >
              <a-radio-group
                v-decorator="['appUploadTemperatureOnOff']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="发送流量信息"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.reportFlowOnOff"
            >
              <a-radio-group
                v-decorator="['reportFlowOnOff']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-tab-pane>
          <!-- 高级配置 -->
          <a-tab-pane tab="高级配置" :forceRender="true" key="2" style="margin-top: 35px">
            <a-form-item
              label="算法分组号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micGroupId"
              has-feedback
            >
              <a-input
                placeholder="请输入算法分组号"
                v-decorator="['micGroupId']"/>
            </a-form-item>

            <a-form-item
              label="算法ID"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micAccessToken"
              has-feedback
            >
              <a-input
                placeholder="请输入算法ID"
                v-decorator="['micAccessToken']"/>
            </a-form-item>

            <a-form-item
              label="通道号生成基础数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micRouterBase"
              has-feedback
            >
              <a-input-number
                placeholder="通道号生成基础数"
                style="width: 100%"
                v-decorator="['micRouterBase', { initialValue: '0' }]"
                :min="0"
                :max="7"
                :step="1"/>
            </a-form-item>

            <a-form-item
              label="USB模式是否屏蔽default设备"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micIsShieldDefaultDevice"
            >
              <a-radio-group
                v-decorator="['micIsShieldDefaultDevice']">
                <a-radio value="true">是</a-radio>
                <a-radio value="false">否</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="TCP模式下IP地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micTcpIp"
              has-feedback
            >
              <a-input
                placeholder="请输入TCP模式下IP地址"
                v-decorator="['micTcpIp']"/>
            </a-form-item>

            <a-form-item
              label="TCP模式下端口号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micTcpPort"
              has-feedback
            >
              <a-input-number
                placeholder="端口号"
                style="width: 100%"
                v-decorator="['micTcpPort']"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micDiskCapacity"
              label="存储剩余大小(%)"
            >
              <a-input-number
                placeholder="95"
                style="width: 100%"
                v-decorator="['micDiskCapacity', { initialValue: '95' }]"
                :min="5"
                :max="100"
                :step="5"/>
            </a-form-item>

            <a-form-item
              label="文件存储模式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micFileStorageMode"
            >
              <a-radio-group
                v-decorator="['micFileStorageMode',{ initialValue: 'local' }]">
                <a-radio value="local">本地</a-radio>
                <a-radio value="remote">远程</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micMaxFrequencyVoiceRatio"
              label="音频过载判断比例(%)"
            >
              <a-input-number
                placeholder="80"
                style="width: 100%"
                v-decorator="['micMaxFrequencyVoiceRatio', { initialValue: '80' }]"
                :min="10"
                :max="100"
                :step="10"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micNoFrequencyVoiceRatio"
              label="音频无声判断比例(%)"
            >
              <a-input-number
                placeholder="20"
                style="width: 100%"
                v-decorator="['micNoFrequencyVoiceRatio', { initialValue: '20' }]"
                :min="10"
                :max="100"
                :step="10"/>
            </a-form-item>

            <a-form-item
              label="文件无声或过载是否删除文件"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.micFileErrorIsDelete"
            >
              <a-radio-group
                v-decorator="['micFileErrorIsDelete',{ initialValue: 'true' }]">
                <a-radio value="true">是</a-radio>
                <a-radio value="false">否</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="FTP文件扫描路径扫描路径"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audioScanPath"
              has-feedback
            >
              <a-input
                placeholder="FTP文件扫描路径扫描路径"
                v-decorator="['audioScanPath']"/>
            </a-form-item>

            <a-form-item
              label="引擎地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.engineHttpUrl"
              has-feedback
            >
              <a-input
                placeholder="引擎地址"
                v-decorator="['engineHttpUrl']"/>
            </a-form-item>

            <a-form-item
              label="文件存储服务器地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.engineFileSaveUrl"
              has-feedback
            >
              <a-input
                placeholder="文件存储服务器地址"
                v-decorator="['engineFileSaveUrl']"/>
            </a-form-item>

            <a-form-item
              label="是否启用倾角设备"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.sensorOnOrOff"
            >
              <a-radio-group
                v-decorator="['sensorOnOrOff',{ initialValue: 'off' }]">
                <a-radio value="on">是</a-radio>
                <a-radio value="off">否</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="倾角传感器串口号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.sensorSerialCom"
              has-feedback
            >
              <a-input
                placeholder="倾角传感器串口号"
                v-decorator="['sensorSerialCom']"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.sensorSensorSampleRate"
              label="传感器采集速率(ms)"
            >
              <a-input-number
                placeholder="1000"
                style="width: 100%"
                v-decorator="['sensorSensorSampleRate', { initialValue: '1000' }]"
                :min="1000"
                :step="1000"/>
            </a-form-item>

            <a-form-item
              label="8路采集板串口控制开关"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audiocaptureOnOrOff"
            >
              <a-radio-group
                v-decorator="['audiocaptureOnOrOff']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="8路采集板串口名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audiocaptureSerialName"
              has-feedback
            >
              <a-input
                placeholder="8路采集板串口名称"
                v-decorator="['audiocaptureSerialName',{ initialValue: 'ttyXRUSB0' }]"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="8路采集板录音时间"
              v-show="!!setData.sensorSensorSampleRate"
            >
              <a-input-number
                style="width: 100%"
                v-decorator="['sensorSensorSampleRate']"
                :min="7"
                :max="35"
                :step="1"/>
            </a-form-item>

            <a-form-item
              label="主动发声开关"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.soundmakerOnOrOff"
            >
              <a-radio-group v-decorator="['soundmakerOnOrOff']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="主动发声频率"
              v-show="!!setData.soundmakerFrequency"
            >
              <a-input-number
                style="width: 100%"
                v-decorator="['soundmakerFrequency']"
                :min="50"
                :max="20000"
                :step="50"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.audiocaptureRecordingTime"
              label="8路采集板录音时间"
            >
              <a-input-number
                placeholder="15"
                style="width: 100%"
                v-decorator="['audiocaptureRecordingTime', { initialValue: '15' }]"
                :min="7"
                :max="30"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.soundmakerVolumeActiveVoice"
              label="主动发声音量"
            >
              <a-input-number
                placeholder="10"
                style="width: 100%"
                v-decorator="['soundmakerVolumeActiveVoice', { initialValue: '10' }]"
                :min="1"
                :max="10"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="主动发声工作时间(s)"
              v-show="!!setData.soundmakerWorkingTime"
            >
              <a-input-number
                placeholder="10"
                style="width: 100%"
                v-decorator="['soundmakerWorkingTime', { initialValue: '10' }]"
                :min="7"
                :max="30"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.soundmakerIntermittentTime"
              label="主动发声间隔时间(s)"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['soundmakerIntermittentTime', { initialValue: '60' }]"
                :min="0"
                :step="1"/>
            </a-form-item>

            <a-form-item
              label="主动发声设备名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.soundmakerPlayVoiceDeviceName"
              has-feedback
            >
              <a-input
                placeholder="请输入主动发声设备名称"
                v-decorator="['soundmakerPlayVoiceDeviceName']"/>
            </a-form-item>

            <a-form-item
              label="光纤服务开关"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalOnOrOff"
            >
              <a-radio-group v-decorator="['opticalOnOrOff']">
                <a-radio value="on">开</a-radio>
                <a-radio value="off">关</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="光纤服务URL"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalServerUrl"
              has-feedback
            >
              <a-input
                placeholder="请输入光纤服务URL（http://ip:port/）"
                v-decorator="['opticalServerUrl',{ initialValue: 'http://192.168.1.2:8000/' }]"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalVibrationPort"
              label="光纤振动数据采集服务端口"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['opticalVibrationPort', { initialValue: '4070' }]"
                :min="80"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalAudioPort"
              label="光纤音频数据采集服务端口"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['opticalAudioPort', { initialValue: '4060' }]"
                :min="80"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalListeningLocation"
              label="光纤侦听点位"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['opticalListeningLocation', { initialValue: '2745' }]"
                :min="200"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalDiff"
              label="光纤采样率配置"
            >
              <a-radio-group v-decorator="['opticalDiff', { initialValue: '100' }]">
                <a-radio value="40">40</a-radio>
                <a-radio value="80">80（8k）</a-radio>
                <a-radio value="100">100（16k）</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalDistance"
              label="光纤侦听距离（Km）"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['opticalDistance', { initialValue: '2' }]"
                :min="1"
                :step="1"/>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalNoiseReductionLevel"
              label="光纤降噪等级"
            >
              <a-radio-group v-decorator="['opticalNoiseReductionLevel', { initialValue: '2' }]">
                <a-radio value="1">1级</a-radio>
                <a-radio value="2">2级</a-radio>
                <a-radio value="3">3级</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              label="光纤降噪开关"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalNoiseReductionSwitch"
            >
              <a-radio-group
                v-decorator="['opticalNoiseReductionSwitch']">
                <a-radio value="true">开</a-radio>
                <a-radio value="false">关</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalScanRate"
              label="光纤音频采样率（Hz）"
            >
              <a-radio-group v-decorator="['opticalScanRate', { initialValue: '16000' }]">
                <a-radio value="16000">16000</a-radio>
                <a-radio value="8000">8000</a-radio>
              </a-radio-group>
            </a-form-item>

            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              v-show="!!setData.opticalVibrationThreshold"
              label="光纤振动数据报警阈值"
            >
              <a-input-number
                placeholder="60"
                style="width: 100%"
                v-decorator="['opticalVibrationThreshold', { initialValue: '300' }]"
                :min="0"
                :step="1"/>
            </a-form-item>

          </a-tab-pane>
        </a-tabs>
      </a-form>

    </div>

  </a-modal>
</template>

<script>
  import { STable } from '@/components'
  import addForm from './addForm'
  import editForm from './editForm'
  import { getConfig, updateConfig } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 }
        },
        form: this.$form.createForm(this),
        soundCardList: [],
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        addTitle: '',
        setData: {},
        formLoading: false,
        confirmLoading: false,
        gatewayId: '',
        visible: false,
        micGroupId: '',
        micAccessToken: '',
        micRouterBase: [0, 1, 2, 3, 4, 5, 6, 7],
        micDeviceType: [{ type: 'tcp' }, { type: 'ftb' }, { type: 'usb' }],
        micUsbMode: [{ model: '有1:N过程', value: 1 }, { model: '无1:N过程', value: 2 }]
      }
    },

    methods: {
      toIndex (record) {
        this.addTitle = record.gatewayName || ''
        this.visible = true
        this.gatewayId = record.id
        this.queryParam.gatewayId = record.id
        this.resetValue()
      },
      resetValue () {
        const that = this
        getConfig({
          id: this.gatewayId
        }).then((res) => {
          this.setData = res.data

          const data = res.data
          Object.keys(that.form.getFieldsValue()).forEach(item => {
            const value = data[item]
            that.form.setFieldsValue({ [item]: value })
          })
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          this.confirmLoading = true
          if (!errors) {
            values.id = this.gatewayId
            updateConfig(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('修改成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('修改失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
